package grape.crm.service.department.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.common.service.trans.ITransService;
import grape.crm.service.department.po.Department;
import grape.common.service.common.IBaseTreeService;

/**
 * <p>
 * 部门表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
public interface IDepartmentService extends IBaseTreeService<Department> , ITransService<String,String> {

    public static final String trans_department_name = "trans_department_name";
    public static final String trans_department_code = "trans_department_code";
    public static final String trans_department_parent_name = "trans_department_parent_name";

    /**
     * 判断编码是否已存在
     * @param code
     * @return
     */
    default boolean existCode(String code){
        assertParamNotEmpty(code,"code 不能为空");
        Department department = new Department();
        department.setCode(code);
        int r = count(Wrappers.query(department));
        return r > 0;
    }
    @Override
    default boolean support(String type){
        return isEqualAny(type,trans_department_code, trans_department_name, trans_department_parent_name);
    }

    @Override
    default String trans(String type, String key){
        if (isEqual(type,trans_department_parent_name)) {
            Department parent = getParent(key);
            if (parent != null) {
                return parent.getName();
            }
        }else{
            Department po = getById(key);
            if (po != null) {
                if (isEqual(type,trans_department_name)) {
                    return po.getName();
                }else if (isEqual(type,trans_department_code)) {
                    return po.getCode();
                }
            }

        }
        return null;
    }
}
